package com.example.homework8;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

public class Sqlite extends Fragment {

    public Sqlite(){
        super(R.layout.sqlite);
    }

    public void onViewCreated(@NonNull View view, @Nullable Bundle saveInstanceState) {
        super.onViewCreated(view, saveInstanceState);

        EditText Name = getActivity().findViewById(R.id.name);
        EditText Age = getActivity().findViewById(R.id.age);
        TextView output = getActivity().findViewById(R.id.output);
        Button insert = (Button)getActivity().findViewById(R.id.insert);
        Button query = (Button)getActivity().findViewById(R.id.query);

        DbHelper dbHelper = new DbHelper(getContext());
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        StringBuilder stringBuilder = new StringBuilder();

        insert.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SQLiteDatabase db = dbHelper.getWritableDatabase();
                ContentValues values = new ContentValues();
                values.put("username", Name.getText().toString());
                values.put("age", Integer.parseInt(Age.getText().toString()));
                long ret = db.insert("users", null, values);
                String msg = ret == -1 ? "添加失败!" : "添加成功!";
                Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();
                db.close();
                db = dbHelper.getReadableDatabase();
                StringBuilder stringBuilder = new StringBuilder();
                Cursor cursor = db.query("users", null, null, null, null, null, null);
                    while (cursor.moveToNext()) {
                        long id = cursor.getLong(cursor.getColumnIndex("id"));
                        String username = cursor.getString(cursor.getColumnIndex("username"));
                        int age = cursor.getInt(cursor.getColumnIndex("age"));
                        stringBuilder.append("id:").append(id)
                                .append("  username:").append(username)
                                .append("  age:").append(age)
                                .append("\n");

                    }
                output.setText(stringBuilder.toString());
                }
        });

        query.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SQLiteDatabase db = dbHelper.getReadableDatabase();
                StringBuilder stringBuilder = new StringBuilder();
                Cursor cursor = db.query("users", null, "age < 18", null, null, null, null);
                    while (cursor.moveToNext()) {
                        long id = cursor.getLong(cursor.getColumnIndex("id"));
                        String username = cursor.getString(cursor.getColumnIndex("username"));
                        int age = cursor.getInt(cursor.getColumnIndex("age"));
                        stringBuilder.append("id:").append(id)
                                .append("  username:").append(username)
                                .append("  age:").append(age)
                                .append("\n");

                    }
                output.setText(stringBuilder.toString());
                }
        });
    }

    public void AllQuery(){

    }
}